import 'package:flutter/material.dart';
import 'package:flutter_desktop_template/router/routes.dart';
import 'package:get/get.dart';

class LeftNavBar extends StatefulWidget {
    const LeftNavBar({super.key});

    @override
    State<LeftNavBar> createState() => _LeftNavBarState();
}

class _LeftNavBarState extends State<LeftNavBar> {
    int currentIndex=0;

    List<GetPage<dynamic>> menuRoutes=[];

    @override
    void initState() {
        super.initState();
        for (var item in routes[0].children) {
            var hide=(item.arguments as Map)['hide'];
            if(hide==null||hide!=true){
                menuRoutes.add(item);
            }
        }
    }

    @override
    Widget build(BuildContext context) {
        return NavigationRail(
            destinations:menuRoutes.asMap().entries.map((entry) {
                int index=entry.key;
                return NavigationRailDestination(
                    icon: Icon((menuRoutes[index].arguments as Map)['icon']),
                    // icon: const Icon(Icons.home),
                    label: Text(menuRoutes[index].title??"")
                );
            }).toList(),
            selectedIndex: currentIndex,
            labelType: NavigationRailLabelType.all,
            onDestinationSelected:(index){
                Get.toNamed(menuRoutes[index].name);
                setState(() {
                    currentIndex=index;
                });
            }
        );
    }
}